#include "ftpushbuttonicon.h"
#include "ftcontrols.h"

FTPushButtonIcon::FTPushButtonIcon(const QIcon& i, FTControls::IconSize s, QWidget *parent) :
    QAbstractButton(parent)
{
    initialize();
    setIcon(i);
    QAbstractButton::setIconSize( FTControls::iconSize( s ));
}

FTPushButtonIcon::FTPushButtonIcon(QWidget *parent) :
    QAbstractButton(parent)
{
    initialize();
}

void FTPushButtonIcon::setIconSize(FTControls::IconSize s)
{
    QAbstractButton::setIconSize( FTControls::iconSize( s ) );
}

void FTPushButtonIcon::initialize()
{
    setAttribute( Qt::WA_NoMousePropagation, true);
    setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
}

QSize FTPushButtonIcon::sizeHint() const
{
    int min_height = FTControls::defaultHeight() / 2;
    QSize size = iconSize();
    if ( size.width() < min_height )
        size.setWidth( min_height );
    if ( size.height() < min_height )
        size.setHeight( min_height );
    return size;
}

QSize FTPushButtonIcon::minimumSizeHint() const
{
    int min_height = FTControls::defaultHeight() / 2;
    return QSize( min_height, min_height );
}

void FTPushButtonIcon::paintEvent( QPaintEvent * )
{
    bool buttonCheck = isDown() || isChecked();
    QPalette palette = QApplication::palette();
    QPainter painter(this);

    palette.setCurrentColorGroup( FT_COLOR_GROUP );
    QPen buttonPen =  palette.color( buttonCheck ? QPalette::HighlightedText : QPalette::Light );
    QBrush buttonBrush =  buttonCheck ? palette.brush( QPalette::Highlight ) :
                                        ( isEnabled() ? QBrush( Qt::NoBrush ) : palette.brush( QPalette::Dark ) );

    // This is the border of the button
    QRect areaRect = rect();
    QRect borderRect = areaRect.adjusted(1,1,-1,-1);

    painter.fillRect( areaRect, buttonBrush );

    // Set default pen for the border and draw the border
    painter.setPen( buttonPen );
    painter.drawRect( borderRect );

    // Paint the icon!
    icon().paint( &painter, rect().adjusted( 3,3,-3,-3 ), Qt::AlignCenter | Qt::AlignVCenter );
}

